<?php

require_once("header.php");

$smarty->assign('topicList', getTopicListCode());

$id = $_GET['id'];
// check post id
if ( !preg_match("/^[0-9]{1,10}$/", $id) ) {
	$id = 1;
}

$sql = "Select * From post Where post_id = $id";

$result = $db->query($sql);

$comm = array();
$commNo = 0;
foreach ( $result as $row ) {

	// all votes number
	$all = 0;
	$all_r = $db->query("SELECT Count(*) As Total FROM vote WHERE post_id = ".$row['post_id']);
	if ( $all_r ) {
		foreach ( $all_r as $r ) {
			$all = $r['Total'];
			break;
		}
	}
	
	$support = 0;
	$vote = 0;
	$supportrate = '10,10,10,10';
	
	if ( $all > 0 ) {
	
		// pro votes number
		$support_r = $db->query("SELECT Count(*) As Support FROM vote WHERE result = 1 And post_id = ".$row['post_id']);
		if ( $support_r ) {
			foreach ( $support_r as $r ) {
				$support = $r['Support'];
				break;
			}
		}
		// current user choose
		if ( $_SESSION['logon'] === true ) {
			$vote_r = $db->query("SELECT result FROM vote WHERE user_id = ".$_SESSION['user_id']." And post_id = ".$row['post_id']);
			if ( $vote_r ) {
				foreach ( $vote_r as $r ) {
					$vote = $r['result'];
					break;
				}
			}
		}
		
		// support rate by gender
		$supportrate = getSupport(true, true, $row['post_id']) . ',' . getSupport(true, false, $row['post_id']) . ',' . getSupport(false, true, $row['post_id']) . ',' .getSupport(false, false, $row['post_id']);
		
	}
	
	$favourite = '';
	// if favourited
	if ( $_SESSION['logon'] === true ) {
		if ( $topic == -1 || count( $db->query("SELECT post_id FROM favourite WHERE user_id = ".$_SESSION['user_id']." And post_id = ".$row['post_id'])->fetchAll() ) > 0 ) {
			$favourite = 'checked="checked"';
		}
	}
	// support rate
	$rate = ( $all == 0 ? "50%" : round($support / $all * 100)."%" );
	
	$results[] = array(
		'topicid' => $row['topic_id'],
		'postid' => $row['post_id'],
		'content' => htmlspecialchars(stripslashes($row['content'])),
		'posttime' => $row['post_time'],
		'userid' => $row['user_id'],
		'rate' => $rate,
		'pro' => ( $vote > 0 ? " voted" : "" ),
		'can' => ( $vote < 0 ? " voted" : "" ),
		'favourite' => $favourite
	);
	
	
	$comments = $db->query('Select * From comment Where post_id = '.$row['post_id'].' Order By comment_time Asc');
	foreach ( $comments as $comment ) {
		$commNo++;
		$comm[] = array(
			'content' => htmlspecialchars(stripslashes($comment['content']))
		);
	
	}
	break;

}

$nocomm = ($commNo > 0 ? '' : 'No comments.');

$smarty->assign('nocomment', $nocomm);

$smarty->assign('comments', $comm);

$smarty->assign('supportrate', $supportrate);
$smarty->assign('results', $results);

$smarty->display('viewpost.html');

function getSupport($male, $pro, $postid) {

	global $db;
	
	$count = 0;
	
	$vote = $db->query("SELECT Count(*) As s FROM vote WHERE user_id In (Select user_id From user Where gender = ".( !!$male ? 1 : 2 ).") And result ".( !!$pro ? '>' : '<' )." 0 And post_id = ".$postid);
	if ( $vote ) {
		foreach ( $vote as $r ) {
			$count = $r['s'];
			break;
		}
	}
		
	return $count;

}

?>